<template>
  <div class="container bgc">
    <div class="w1300 flex-xbt">
      <side-bar @getBarData="getBarData" @getChooseId="getChooseId"></side-bar>
      <div class="main">
        <div class="search-box flex-xyc">
          <div class="search-wrap flex-yc">
            <el-icon size="18" color="#3AACE3" class="iconfont">
              <Search />
            </el-icon>
            <input class="input" v-model="keyWords" placeholder="请输入搜索内容" />
            <el-button class="btn" size="large" type="primary" round @click="searchKeywords">
              搜索
            </el-button>
          </div>
          <el-button
            class="btn-huati"
            type="primary"
            size="large"
            @click="onAddHuaTi"
          >
            添加话题
          </el-button>
        </div>

        <put-hua-ti :totalBarData="barData"></put-hua-ti>
        <div class="container-wrap" v-if="article.length !== 0" v-for="item in article" :key="item.id">
          <article-com :article="item">
            <div
              class="comment-img-wrap flex-yc a"
              @click="$router.push('/forum/index/detail')"
            >
              <img src="" />
              <img src="" />
              <img src="" />
              <img src="" />
            </div>
          </article-com>
        </div>
        <div class="container-wrap empty-box" v-else>
          <el-empty :image-size="150"  description="暂无结果"/>
        </div>
        <div class="loading-more-box" ref="loadMore"></div>
      </div>
      <user-right-hot></user-right-hot>
    </div>
  </div>
</template>

<script setup lang="ts">
import SideBar from '@/pages/forum/views/index/components/SideBar.vue'
import UserRightHot from '@/pages/forum/views/index/components/UserRightHot.vue'
import ArticleCom from '@/pages/forum/components/ArticleCom.vue'
import {LoadBoardList} from "@/pages/forum/types/loadBoard";
import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
import { ElMessageBox } from 'element-plus'
import { articleType } from '@/pages/forum/types/article.ts'
import { MsgError, MsgSuccess } from '@/utils/utils.ts'
import { reqCreateHuati, getToltalArticle, searchData } from '@/pages/forum/api/huati.ts'
import PutHuaTi from '@/pages/forum/views/index/components/PutHuaTi.vue'
const instance = getCurrentInstance();
let article = reactive<articleType[]>([])
let barData = reactive<LoadBoardList[]>([]);
let pageTotal = ref<number>(1);
let currentPageNum = ref<number>(1);
let keyWords = ref<string>('');
let chooseId = ref<number>(0);
// 添加话题
const onAddHuaTi = () => {
  ElMessageBox.prompt('请输入话题', '', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern: /.{1,50}/,
    inputErrorMessage: '话题内容过长',
  }).then(async ({ value }) => {
    const { status, info } = await reqCreateHuati({ code: '', name: value })
    if (status === 'success') MsgSuccess('添加成功')
    else MsgError(info || '添加失败')
  })
}
//获取分类数据
function getBarData(data) {
  data.map((i) => {
    barData.push(i);
  })
}
//加载更多
function loadMoreData() {
  const intersectionObserver = new IntersectionObserver((entries) => {
    if (article.length === 0) return;
    console.log(entries[0])
    if (entries[0].isIntersecting) {
      ++currentPageNum.value;
      if (currentPageNum.value <= pageTotal.value) {
        getTotalArticle(chooseId.value, pageTotal.value, true);
      } else {
        currentPageNum.value = 1;
        MsgSuccess('暂无更多数据');
        intersectionObserver.unobserve(instance.refs.loadMore);
      }
    }
  });
// 开始监听
  intersectionObserver.observe(instance.refs.loadMore);
}
//获取当前id
async function getChooseId(value) {
  chooseId.value = value;
  getTotalArticle(chooseId.value, 1, false);
  //重新触发监听
  loadMoreData();
}
//查询列表
async function getTotalArticle(id: number = 0 ,pageNum, concatFlag:boolean = false) {
  let result = await getToltalArticle({
    pageNo: pageNum,
    boardId: id,
    pBoardId: 0,
    pageSize: 15
  });
  if (!result || result.code !== 200) return MsgError(result?.msg || '请求错误');
  console.log(result, 'get----')
  pageTotal.value = result.data.pageTotal;
  if (!concatFlag) {
    article.length = [];
  }
  result.data.list.map((item) => {
    article.push(item);
  })
}
//搜索
async function searchKeywords() {
  let result = await searchData(keyWords.value);
  //todo 分页
  if (!result || result.code !== 200) return MsgError(result?.msg || '请求错误');
  article.length = [];
  result.data.list.map((item) => {
    article.push(item);
  })
}
onMounted(() => {
  getTotalArticle(chooseId.value, pageTotal.value);
  loadMoreData();
})
</script>

<style scoped lang="scss">
.container {
  min-height: 100vh;
  overflow: hidden;

  .w1300 {
    padding: 20px 0;

    .main {
      width: 840px;

      .search-box {
        padding: 28px 36px;
        background-color: #fff;
        border-radius: 4px;

        .search-wrap {
          width: 765px;
          height: 51px;
          padding: 5px 5px 5px 21px;
          font-size: 14px;
          border: 1px solid $primary;
          border-radius: 100px;

          input {
            width: 100%;
            height: 100%;
            margin-left: 15px;
            border: none;
          }

          .btn {
            flex-shrink: 0;
            width: 85px;
          }
        }
        .btn-huati {
          flex-shrink: 0;
          margin-left: 10px;
        }
      }

      .container-wrap {
        padding: 22px 25px;
        margin-top: 16px;
        background-color: #fff;
        border-radius: 4px;
        overflow-y: auto;
        .comment-img-wrap {
          flex-wrap: wrap;
          justify-content: space-between;

          img {
            width: 188px;
            height: 162px;
            background-color: #d9d9d9;
            border-radius: 4px;
          }
        }
      }
    }
  }
}
</style>
